package cn.yunhe.news.servlet;

import cn.yunhe.news.dao.IAdminDao;
import cn.yunhe.news.dao.impl.AdminDao;
import cn.yunhe.news.entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        PrintWriter out = response.getWriter();

        String name = request.getParameter("loginId");
        String pass = request.getParameter("password");

        IAdminDao adminDao = new AdminDao();
        Admin admin = adminDao.getAdmin(name, pass);


        if (admin != null) {
            request.getSession().setAttribute("admin", admin);
            request.getRequestDispatcher("mainform.jsp").forward(request, response);
        } else {
            out.print("<script language='JavaScript'>alert('账号或密码输入错误请重新输入!!!');</script>");
            response.sendRedirect("index.html");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
